package com.starbaba.log4j.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.log4j.model.Log4jCountBean;
import com.starbaba.log4j.model.Log4jDetailBean;
import com.starbaba.log4j.service.Log4jStatisticsService;
import com.starbaba.model.dictionary.Constant;
import com.tuanzi.server.loan_base.util.DateUtils;

/**
 * 日志统计Service
 * @author chenweijun
 *
 */
public class LogStatisticsServiceImpl implements Log4jStatisticsService{
	
	private BaseDao baseDao;
	public LogStatisticsServiceImpl(){
		baseDao = new BaseDao(Constant.COMPANY_DB_POOL);
	}

	@Override
	public List<Log4jCountBean> getLog4jList(Map<String, Object> paramsMap,
			int pageNo, int pageSize) {
		
		String sql = generateGetListSql(paramsMap) + "LIMIT " + pageNo + "," +  pageSize;
		List<Log4jCountBean> list = new ArrayList<Log4jCountBean>();
		try {
			list = baseDao.list(sql, Log4jCountBean.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}
	
	@Override
	public int getLog4jCount(Map<String, Object> paramsMap) {
		
		String sql = "SELECT COUNT(1) FROM (" + generateGetListSql(paramsMap) + ") AS a";
		int result = 0;
		try {
			result = baseDao.getInt(sql);
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}

	@Override
	public Log4jDetailBean getLog4fDetail(Map<String, Object> paramsMap) {
		
		String createDay = (String) paramsMap.get("createDay"); 
		String projectname = (String) paramsMap.get("projectname");
		String firststack = (String) paramsMap.get("firststack");
		
		StringBuilder sql = new StringBuilder("SELECT `projectname`,`exception`,`firststack`,`stacktrace`,`msg` FROM `log4j` WHERE  ");
		sql.append(" `createdate` BETWEEN '" + createDay + " 00:00:00'" + " AND '" + createDay + " 23:59:59' ")
		.append(" AND `projectname` = '" + projectname + "' ")
		.append(" AND `firststack` LIKE '%" + firststack + "%' ")
		.append(" LIMIT 1");
		
		Log4jDetailBean detailBean = null;
		try {
			detailBean = baseDao.get(sql.toString(), Log4jDetailBean.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		return detailBean;
	}
	
	/**
	 * 生成查询列表的sql
	 * @param paramsMap
	 * @return
	 */
	public String generateGetListSql(Map<String, Object> paramsMap) {
		
		String begin_date = (String) paramsMap.get("begin_date");
		String end_date = (String) paramsMap.get("end_date");
		String projectname = (String) paramsMap.get("projectname");
		
		StringBuilder sql = new StringBuilder("SELECT DATE_FORMAT(`createdate`,'%Y-%m-%d') AS `createDay`, "
				+ "`projectname`, `firststack` , COUNT(1) as `size` FROM `log4j` WHERE 1 = 1 ");
		if(StringUtils.isNotEmpty(begin_date)){
			sql.append(" AND createdate >= '" + begin_date + " 00:00:00" + "'");
		}else{
			sql.append(" AND createdate >= '" + DateUtils.getDateBeforeDays(1) + " 00:00:00" + "'");
		}
		
		if(StringUtils.isNotEmpty(end_date)){
			sql.append(" AND createdate <= '" + end_date + " 23:59:59" + "'");
		}else{
			sql.append(" AND createdate<= '" + DateUtils.getCurrentDate() + " 23:59:59" + "'");
		}
		
		if(StringUtils.isNotEmpty(projectname)){
			sql.append(" AND projectname = '" + projectname + "'");
		}else{
			sql.append(" AND `projectname` in ('loan_database', 'loan_service', 'loan_source', "
					+ "'loan_timer', 'loan_user', 'loan_utils', 'loan_weixin', 'loan_timer', "
					+ "'loan_external_login', 'loan_template_service') ");
		}
		
		sql.append(" GROUP BY `projectname`,`createDay`,`firststack` ORDER BY `createDay` DESC, `size` DESC ");
		
		return sql.toString();
	}


}
